package StructuralMode.proxy.proxyDynamic.railwayStation;

import java.lang.reflect.Proxy;

public class DynamicProxy {
   private RailwayStation railwayStation = new RailwayStationReal();
   public RailwayStation createStationProxy(){
       return (RailwayStation) Proxy.newProxyInstance(
               railwayStation.getClass().getClassLoader(),
               railwayStation.getClass().getInterfaces(),
               (proxy, method, args) -> {
                   if(method.getName() == "sell"){
                       System.out.println("代理类执行买票前的操作");
                       Object res = method.invoke(railwayStation,args);
                       System.out.println("代理类执行买票后的操作");
                       return res;
                   }
                   return method.invoke(args);
               });
   }
}
